package jp.co.dailyReport;

import java.io.*;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.total.TotalHoursWorkedStaffResult;
import jp.co.dailyReport.total.TotalHoursWorkedProjectResult;
import jp.co.dailyReport.expense.ExpenseManufacturingResult;
import jp.co.dailyReport.total.TotalHoursWorkedStaffManager;
import jp.co.dailyReport.total.TotalHoursWorkedProjectManager;

/**
 * CSV形式ファイル出力情報管理クラス
 */
public class CSVManager {

	private static Logger log = Logger.getLogger(CSVManager.class);

	/**
	 * 社員毎稼働時間集計結果情報をCSV形式ファイルで、
	 * 出力する処理を行います。
	 * 
	 * @param response HTTPレスポンス
	 * @param totalHoursWorkedStaffResult 社員毎稼働時間集計結果オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void CSVOutput(HttpServletResponse response,TotalHoursWorkedStaffResult[] totalHoursWorkedStaffResult) 
		throws DataUnavailableException {
		log.debug("CSVOutput start");

		BufferedOutputStream bos = null;
		try{
			String fileName = "CSVDATA_TOTALHOURSWORKED_STAFF.csv";
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=\""+fileName+"\"");
			bos = new BufferedOutputStream(response.getOutputStream());
			java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy年MM月dd日");

			bos.write("稼働時間合計(h)".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			String totalHoursWorkedStaff = String.valueOf(TotalHoursWorkedStaffManager.calculate(totalHoursWorkedStaffResult));
			bos.write(totalHoursWorkedStaff.getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			bos.write("氏名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("雇用形態".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("日付".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("ﾌﾟﾛｼﾞｪｸﾄ名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("作業項目名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("稼働時間(h)".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("フラグ".getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			for(int i=0;i<totalHoursWorkedStaffResult.length;i++){
				bos.write(Utilities.changeUniqueCharacter("\"" + totalHoursWorkedStaffResult[i].getStaffName() + "\"").getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				String employmentSystemName = Staff.STAFF_EMPLOYMENTSYSTEM[totalHoursWorkedStaffResult[i].getEmploymentSystem()];
				bos.write(employmentSystemName.getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				bos.write(df.format(totalHoursWorkedStaffResult[i].getWorkDate()).getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				//日報登録時、作業項目を一つも入力せずに登録した時の対応策
				if(totalHoursWorkedStaffResult[i].getProjectName()!=null){
					bos.write(Utilities.changeUniqueCharacter("\"" + totalHoursWorkedStaffResult[i].getProjectName() + "\"").getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					bos.write(Utilities.changeUniqueCharacter("\"" + totalHoursWorkedStaffResult[i].getWorkItemName() + "\"").getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					String workingHours = String.valueOf(totalHoursWorkedStaffResult[i].getWorkingHours());
					bos.write(workingHours.getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					bos.write(Utilities.changeUniqueCharacter("\"" + totalHoursWorkedStaffResult[i].getErrorFlg() + "\"").getBytes("SJIS"));
				}
				bos.write("\n".getBytes("SJIS"));
			}
			bos.flush();
			bos.close();
		} catch(IOException ioe) {
			throw new DataUnavailableException(ioe.toString());
		}
		log.debug("CSVOutput end");
	}

	/**
	 * プロジェクト毎稼働時間集計結果情報を
	 * CSV形式ファイルで、出力する処理を行います。
	 * 
	 * @param response HTTPレスポンス
	 * @param totalHoursWorkedProjectResult プロジェクト毎稼働時間集計結果オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void CSVOutput(HttpServletResponse response,TotalHoursWorkedProjectResult[] totalHoursWorkedProjectResult) 
		throws DataUnavailableException {
		log.debug("CSVOutput start");

		BufferedOutputStream bos = null;
		try{
			String fileName = "CSVDATA_TOTALHOURSWORKED_PROJECT.csv";
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=\""+fileName+"\"");
			bos = new BufferedOutputStream(response.getOutputStream());
			java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy年MM月第WW週");

			bos.write("稼働時間合計(h)".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			String totalHoursWorkedStaff = String.valueOf(TotalHoursWorkedProjectManager.calculate(totalHoursWorkedProjectResult));
			bos.write(totalHoursWorkedStaff.getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			bos.write("日付".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("作業項目名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("稼働時間(h)".getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			for(int i=0;i<totalHoursWorkedProjectResult.length;i++){
				bos.write(df.format(totalHoursWorkedProjectResult[i].getTotalWorkDate()).getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				bos.write(Utilities.changeUniqueCharacter("\"" + totalHoursWorkedProjectResult[i].getWorkItemName() + "\"").getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				String workingHours = String.valueOf(totalHoursWorkedProjectResult[i].getWorkingHours());
				bos.write(workingHours.getBytes("SJIS"));
				bos.write("\n".getBytes("SJIS"));
			}
			bos.flush();
			bos.close();
		} catch(IOException ioe) {
			throw new DataUnavailableException(ioe.toString());
		}
		log.debug("CSVOutput end");
	}

	/**
	 * 仕掛計上結果情報をCSV形式ファイルで、
	 * 出力する処理を行います。
	 * 
	 * @param response HTTPレスポンス
	 * @param expenseManufacturingResult 仕掛計上結果オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void CSVOutput(HttpServletResponse response,ExpenseManufacturingResult[] expenseManufacturingResult) 
		throws DataUnavailableException {
		log.debug("CSVOutput start");

		BufferedOutputStream bos = null;
		try{
			String fileName = "CSVDATA_EXPENSEMANUFACTURING.csv";
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=\""+fileName+"\"");
			bos = new BufferedOutputStream(response.getOutputStream());
			java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy年MM月dd日");

			bos.write("\n".getBytes("SJIS"));
			bos.write("日付".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("氏名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("取引先名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("ﾌﾟﾛｼﾞｪｸﾄ名".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("稼働時間(h)".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("社員単価×稼働時間(円)".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("作業項目単価×稼働時間(円)".getBytes("SJIS"));
			bos.write(",".getBytes("SJIS"));
			bos.write("\n".getBytes("SJIS"));
			for(int i=0;i<expenseManufacturingResult.length;i++){
				bos.write(df.format(expenseManufacturingResult[i].getWorkDate()).getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				bos.write(Utilities.changeUniqueCharacter("\"" + expenseManufacturingResult[i].getStaffName() + "\"").getBytes("SJIS"));
				bos.write(",".getBytes("SJIS"));
				//日報登録時、作業項目を一つも入力せずに登録した時の対応策
				if(expenseManufacturingResult[i].getCustomerName()!=null){
					bos.write(Utilities.changeUniqueCharacter("\"" + expenseManufacturingResult[i].getCustomerName() + "\"").getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					bos.write(Utilities.changeUniqueCharacter("\"" + expenseManufacturingResult[i].getProjectName() + "\"").getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					String workingHours = String.valueOf(expenseManufacturingResult[i].getWorkingHours());
					bos.write(workingHours.getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					String expeseStaff = String.valueOf("\"" + expenseManufacturingResult[i].getExpeseStaff() + "\"");
					bos.write(expeseStaff.getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
					String expeseProject = String.valueOf("\"" + expenseManufacturingResult[i].getExpeseProject() + "\"");
					bos.write(expeseProject.getBytes("SJIS"));
					bos.write(",".getBytes("SJIS"));
				}	
				bos.write("\n".getBytes("SJIS"));
			}
			bos.flush();
			bos.close();
		} catch(IOException ioe) {
			throw new DataUnavailableException(ioe.toString());
		}
		log.debug("CSVOutput end");
	}
}
